Ext.define('cp.view.grid.GestionOs', {
    extend: 'cp.view.grid.GestionBase',
    alias: 'widget.gestionOs',
    
    /* Variables de instancia */
    stage: 'Obra social',
    idProperty: 'idOs',
    deleteUrl: 'bin/os/borrar.php',
    undeleteUrl: 'bin/os/recuperar.php',
    
    initComponent: function(){
        this.callParent(arguments);
        
        this.reconfigure(null,[
            {header: '#', dataIndex: 'idOs', hidden: true},
            {header: 'Nombre', dataIndex: 'nombre', hidden: false, flex: 2},
            {header: 'Domicilio', dataIndex: 'domicilio', hidden: false, flex: 1},
            {header: 'Activa', dataIndex: 'activa', hidden: false, flex: 1}
    	]);

        this.getComponent('top-toolbar').add([
            {xtype: 'button', text: 'Editar', iconCls: 'ico-edit', action: 'edit'}
        ]);
    },
    verFormularioAlta: function(){
        var form = Ext.create('cp.view.form.AltaOs',{
            actionUrl: 'bin/os/agregar.php'
        });
        var win = Ext.create('cp.view.ui.DecoratorForm',{
            aTitle: 'Nueva obra social',
            aForm: form
        });
        win.show();
    },
    verFormularioEdicion: function(){
        if(this.hayFilaSeleccionada()){
            if(this.filaSeleccionada().get('activa') === 'Sí'){
                var id = this.filaSeleccionada().get('idOs');
                this.cargarFormularioEdicion(id);
            }else{
                this.mostrarErrItemInactivo();
            }
        }else{
            this.mostrarErrNoSeleccion();
        }
    },
    cargarFormularioEdicion: function(idOs){
        var store = Ext.create('cp.store.form.Os',{});
        Ext.apply(Ext.data.Connection.prototype, {
            async: false
        });
        store.load({params:{idOs: idOs}});
        Ext.apply(Ext.data.Connection.prototype, {
            async: true
        });
        var form = Ext.create('cp.view.form.EditOs',{
            actionUrl: 'bin/os/modificar.php'
        });
        // Ver la forma de pasar este listener al controller
        form.on('afterrender',function(form){
           form.getForm().loadRecord(store.first());
           //form.getForm().findField('idOs').setValue(idOs);
        });
        var win = Ext.create('cp.view.ui.DecoratorForm',{
            aTitle: 'Editar obra social',
            aForm: form
        });
        win.show();
    },
    verFormularioVista: function(){
        if(this.hayFilaSeleccionada()){
            this.cargarFormularioVista(this.filaSeleccionada().get('idOs'));
        }else{
            this.mostrarErrNoSeleccion();
        }
    },
    cargarFormularioVista: function(idOs){
        var store = Ext.create('cp.store.form.Os',{});
        
        Ext.apply(Ext.data.Connection.prototype, {
                async: false
        });
        
        store.load({params:{idOs: idOs}});
        
        Ext.apply(Ext.data.Connection.prototype, {
                async: true
        });
        
        var form = Ext.create('cp.view.form.VerOs',{});
        
        // Ver la forma de pasar este listener al controller
        form.on('afterrender',function(form,idOs){
           form.getForm().loadRecord(store.first());
        });
        var win = Ext.create('cp.view.ui.DecoratorFormSimple',{
            aTitle: 'Ver obra social',
            aForm: form
        });
        win.show();
    },
    borrarItem: function(){
        if(this.hayFilaSeleccionada()){
            Ext.create('cp.view.ui.DeleteConfirm',{
                mensaje: "¿Está seguro de eliminar la " + this.stage.toLowerCase() + " " +
                this.filaSeleccionada().get('nombre') + " del sistema?",
                target: this
            }).showDialog();
        }else{
            this.mostrarErrNoSeleccion();
        }
    },
    recuperarItem: function(){
        if(this.hayFilaSeleccionada()){
            Ext.create('cp.view.ui.UndeleteConfirm',{
                mensaje: "¿Está seguro de recuperar la " + this.stage.toLowerCase() + " " +
                this.filaSeleccionada().get('nombre') + " del sistema?",
                target: this
            }).showDialog();
        }else{
            this.mostrarErrNoSeleccion();
        }
    },
    verFormularioBusqueda: function(){
        var form = Ext.create('cp.view.form.BuscarOs',{
            actionUrl: 'aUrl'
        });
        
        var win = Ext.create('cp.view.ui.DecoratorFormSearch',{
            aTitle: 'Buscar pacientes',
            aForm: form
        });
        
        win.add(form);
        win.show();
    }
});